package code.google.gui;

import java.util.ArrayList;
import java.util.List;

import org.teleal.cling.android.AndroidUpnpServiceImpl;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import code.google.gui.adapter.RegistrateablePagerAdapter;
import code.google.gui.fragments.interfaces.IPlayerDispenser;
import code.google.gui.fragments.interfaces.IPlayerListener;
import code.google.gui.fragments.interfaces.ISearchDispenser;
import code.google.gui.fragments.interfaces.ISearchListener;
import code.google.gui.fragments.interfaces.ISetFragmentListener;
import code.google.model.upnp.UpnpNode;
import code.google.model.util.interfaces.ISong;

import com.example.metamusicgui.R;

public class MetaMusicActivity extends FragmentActivity implements ISetFragmentListener, ISearchDispenser, IPlayerDispenser {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	private RegistrateablePagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	private ViewPager mViewPager;

	private List<ISearchListener> searchListener = new ArrayList<ISearchListener>();
	private List<IPlayerListener> playerListener = new ArrayList<IPlayerListener>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_meta_music);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new RegistrateablePagerAdapter(this);
		mSectionsPagerAdapter.registrateAllFragments(this);
		

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setOffscreenPageLimit(10);
		// Bind the Service necessary for Upnp-Device-Handling
		getApplicationContext().bindService(
				new Intent(this, AndroidUpnpServiceImpl.class),
				UpnpNode.getInstance(), Context.BIND_AUTO_CREATE);
		
		mViewPager.setCurrentItem(2, true);
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.meta_music, menu);
		return true;
	}
	
	@Override
	protected void onStart(){
		super.onStart();
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mSectionsPagerAdapter.deregistrateAllFragments(this);
		//TODO Check if sufficent for unbinding service
		getApplicationContext().unbindService(UpnpNode.getInstance());
	}

	@Override
	public void dispenseSearch(String query) {
		for (ISearchListener listener : searchListener) {
			listener.onSearch(query);
		}
	}

	@Override
	public void addSearchListener(ISearchListener listener) {
		searchListener.add(listener);
	}

	@Override
	public void removeSearchListener(ISearchListener listener) {
		searchListener.remove(listener);
	}

	@Override
	public void dispensePlay(ISong song) {
		for (IPlayerListener listener : playerListener) {
			listener.playSong(song);
		}
		
	}

	@Override
	public void addPlayerListener(IPlayerListener listener) {
		playerListener.add(listener);
	}

	@Override
	public void removePlayerListener(IPlayerListener listener) {
		playerListener.remove(listener);
	}



	@Override
	public void setCurrentFragment(int id) {
		int context = ISetFragmentListener.ID_CONTEXT_MAPPING.get(id);
		int position = ISetFragmentListener.ID_POSITION_MAPPING.get(id);
		// Kontext setzen, sollte zum menu gesprungen worden sein, bleibt der aktuelle kontext erhalten
		switch (context) {
		case ISetFragmentListener.SEARCH_CONTEXT:
			mSectionsPagerAdapter.setSearchAsCurrentContext();
			break;
		case ISetFragmentListener.FILE_CONTEXT:
			mSectionsPagerAdapter.setFilesAsCurrentContext();
			break;
		case ISetFragmentListener.PLAYER_CONTEXT:
			mSectionsPagerAdapter.setPlayerAsCurrentContext();
			break;
		case ISetFragmentListener.SETTING_CONTEXT:
			mSectionsPagerAdapter.setSettingsAsCurrentContext();
			break;
		}
		// position innerhalb des kontextes setzen
		mViewPager.setCurrentItem(position, true);
	}

}
